﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using BF.Common;

namespace BF.Web.pages.UserControls
{
    public class Item
    {
        public string boxLabel = String.Empty;
        public string inputValue = String.Empty;

        public bool Checked = false;
    }

    public partial class MultiComboBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #region 属性

        [Bindable(true)]
        [Category("列数")]
        [DefaultValue("5")]
        [Localizable(true)]
        public int Columns
        {
            get
            {
                return String.IsNullOrEmpty(hidColumns.Value) ? 5 : int.Parse(hidColumns.Value);
            }
            set
            {

                hidColumns.Value = value.ToString();
            }
        }

        /// <summary>
        /// 获取或设置一个值，该值指示是否启用控件。 缺省值:True
        /// </summary>
        public bool Enabled
        {
            get
            {
                return String.IsNullOrEmpty(hidEnabled.Value) ? true : bool.Parse(hidEnabled.Value);
            }
            set
            {
                hidEnabled.Value = value.ToString();
            }
        }

        /// <summary>
        /// 获取或设置控件的宽度。 缺省值：170
        /// </summary>
        [Bindable(true)]
        [Category("宽度")]
        [DefaultValue("170")]
        [Localizable(true)]
        public int Width
        {
            get { return String.IsNullOrEmpty(hidWidth.Value) ? 170 : int.Parse(hidWidth.Value); }
            set { hidWidth.Value = value.ToString(); }
        }
        /// <summary>
        /// 获取或设置控件下拉框容器的宽度。 缺省值：180
        /// </summary>
        public int ListWidth
        {
            get { return string.IsNullOrEmpty(hidListWidth.Value) ? 180 : int.Parse(hidListWidth.Value); }
            set { hidListWidth.Value = value.ToString(); }
        }
        /// <summary>
        /// 获取或设置控件下拉框容器的高度。 缺省值：280
        /// </summary>
        public int ListHeight
        {
            get { return string.IsNullOrEmpty(hidListHeight.Value) ? 280 : int.Parse(hidListHeight.Value); }
            set { hidListHeight.Value = value.ToString(); }
        }

        public ListItem[] SelectedItems
        {
            get
            {
                if (string.IsNullOrEmpty(hidDataSource.Value))
                    return null;

                List<ListItem> tmpList = JsonHelper<Item>.JsonStringToList(hidDataSource.Value)
                   .Where(p => p.Checked)
                   .Select(P => new ListItem()
                   {
                       Text = P.boxLabel,
                       Value = P.inputValue
                   }).ToList();
                    
                return tmpList.ToArray<ListItem>();
            }
        }


        public ListItem[] DataSource
        {
            set
            {
                string str = JsonHelper<Item>.ListToJsonString(value.Select(p => new Item()
                {
                    boxLabel = p.Text,
                    inputValue = p.Value,
                    Checked = p.Selected
                }).ToList<Item>());
                str = str.Replace("\"Checked\":", "\"checked\":");
                hidDataSource.Value = str;
            }
        }


        #endregion 属性
    }
}